import * as Cesium from '@cesium/Source/Cesium.js';
// import trailLinkImage from "@assets/img/6957972-c5f879cd86b79dfd.png";

function drawCanvas() {
  let canvas = document.createElement('canvas');
  canvas.width = 200; //值越大，间距越小
  canvas.height = 1;
  let ctx = canvas.getContext('2d');
  let grd = ctx.createLinearGradient(0, 0, 5, 0);
  grd.addColorStop(0, "rgba(255,255,0,1)");
  grd.addColorStop(0.5, "rgba(255,0,0,1)");
  // grd.addColorStop(0.5, "rgba(255,255,0,0.5)");
  // grd.addColorStop(0.75, "rgba(0,255,0,1)");
  grd.addColorStop(1, "rgba(255,255,0,1)");
  ctx.fillStyle = grd;
  ctx.fillRect(0, 0, 200, 1);
  return canvas.toDataURL("image/png");
}

function drawCanvas2() {
  let canvas = document.createElement('canvas');
  canvas.width = 200; //值越大，间距越小
  canvas.height = 20;
  let ctx = canvas.getContext('2d');
  let grd = ctx.createLinearGradient(0, 0, 20, 0);
  grd.addColorStop(0, "rgba(255,255,0,0.5)");
  grd.addColorStop(0.25, "rgba(0,255,0,1)");
  grd.addColorStop(0.5, "rgba(255,255,0,0.5)");
  grd.addColorStop(0.75, "rgba(0,255,0,1)");
  grd.addColorStop(1, "rgba(255,255,0,0.5)");
  ctx.fillStyle = grd;
  ctx.fillRect(0, 0, 200, 20);
  return canvas.toDataURL("image/png");
}

/*
  流动纹理线
   color 颜色
   duration 持续时间 毫秒
*/
function FlowLineMaterialProperty(color, duration, d) {
  this._definitionChanged = new Cesium.Event();
  this._color = undefined;
  this._colorSubscription = undefined;
  this.color = color;
  this.duration = duration;
  this._d = d;
  this._time = (new Date()).getTime();
  this.isTranslucent = function() {
    return true;
  };
}

Object.defineProperties(FlowLineMaterialProperty.prototype, {
  isConstant: {
    get: function() {
      return false;
    }
  },
  definitionChanged: {
    get: function() {
      return this._definitionChanged;
    }
  },
  color: Cesium.createPropertyDescriptor('color')
});

FlowLineMaterialProperty.prototype.getType = function(time) {
  return 'FlowLine';
}

FlowLineMaterialProperty.prototype.getValue = function(time, result) {
  if (!Cesium.defined(result)) {
    result = {};
  }
  result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
  result.image = Cesium.Material.FlowLineImage;
  result.time = (((new Date().getTime() - this._time) % this.duration) / this.duration) * this._d;
  return result;
}

FlowLineMaterialProperty.prototype.equals = function(other) {
  return this === other || (other instanceof FlowLineMaterialProperty && Property.equals(this._color, other._color))
}

Cesium.Material.FlowLineType = 'FlowLine';
Cesium.Material.FlowLineImage = drawCanvas2();
// Cesium.Material.FlowLineImage = trailLinkImage;
Cesium.Material.FlowLineSource = `czm_material czm_getMaterial(czm_materialInput materialInput){
    czm_material material = czm_getDefaultMaterial(materialInput);
    vec2 st = materialInput.st;
    vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));
    material.alpha = colorImage.a * color.a;
    material.diffuse = (colorImage.rgb+color.rgb)/2.0;
    return material;
  }`;
Cesium.Material._materialCache.addMaterial(Cesium.Material.FlowLineType, {
  fabric: {
    type: Cesium.Material.FlowLineType,
    uniforms: {
      color: new Cesium.Color(0.0, 0.0, 1.0, 0.5),
      image: Cesium.Material.FlowLineImage,
      time: -20
    },
    source: Cesium.Material.FlowLineSource
  },
  translucent: function(material) {
    return true;
  }
});

export default FlowLineMaterialProperty;
